define(function (require, exports, module) {

    WebPDF.Tools.TOOL_NAME_SELANNOT = "Annot Selection Tool";
    var PDFPoint = WebPDF.PDFPoint;
    var PDFRect = WebPDF.PDFRect;

    WebPDF.Tools.AnnotSelectionToolHandler = function (readerApp,annotHandleManager) {
         	var _readerApp = readerApp,
    		_annotHandleManager = annotHandleManager,
    		_rcDraw = new PDFRect(0, 0, 0, 0),
    		_startwindowpoint = new PDFPoint(0, 0),
			_endwindowpoint = new PDFPoint(0, 0),
			_beginSelectionPoint = new PDFPoint(0, 0),
            _endSelectionPoint = new PDFPoint(0, 0);
    		var _currentPageView = null,
    		_self = this,
    		_boundSelectionRectDiv = null,
    		_isMouseDown = false;
    	
       
    	this.getName = function () {
            return WebPDF.Tools.TOOL_NAME_SELANNOT;
        };
        
        this.onInit = function (readerApp, annotHandleManager) {	
        	$.extend(_annotHandleManager,{
            });
        };

        this.onDestroy = function () {
        };
	
        // UI Events
        this.onActivate = function () {
            if (_readerApp.getMainView() != null) {
                var docView = _readerApp.getMainView().getDocView();
                var jqDocViewContainer = $("#"
						+ docView.getDocViewContainerID());
                jqDocViewContainer.css({
                    'cursor': "auto"
                });
            }
           /* var selAnnotCommand = _readerApp.GetCommand("SelectAnnotCommand");
            selAnnotCommand && selAnnotCommand.setState(true);
            */
        };

        this.onDeactivate = function () {
            /*
            var selAnnotCommand = _readerApp.GetCommand("SelectAnnotCommand");
            selAnnotCommand && selAnnotCommand.SetState(false);*/
        };

        this.isEnabled = function () {
            var currentDocument = _readerApp.getMainView().getDocView()
					.getPDFDoc();
            if (currentDocument) {
                if (currentDocument.getDocType() != WebPDF.PDFDocType.XFA
                /*&& _readerApp.GetDocPemission&FPDFPERM_ANNOT_FORM != 0*/)
                    return true;
            }
            return false;
        };

        this.isProcessing = function () {
            return _isMouseDown;
        };

        // Mouse events
        this.onLButtonDown = function (event) {
            var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (!WebPDF.Common.containsNode(jqDocViewContainer.get(0),
					event.target, true)) {
                return false;
            }
            var pDocView = _readerApp.getMainView().getDocView();
            var bShift = event.shiftKey;
            var bCtrl = event.ctrlKey;
            if (!bShift && !bCtrl) {
            	//docView.clearAllSelection();
                _annotHandleManager.clearSelection();
                //pDocView.SetFocusAnnot(NULL);
            }

           // _annotHandleManager.clearDragRects();

            var pageView = null, offsetCurPageView = new PDFPoint(
					0, 0);
            pPageView = WebPDF.TextSelect.CReader_TextSelectTool
					.getPageViewInfoByMouseEvent(_readerApp, event,
							offsetCurPageView, true, false);
            if (pageView == null)
                return false;
            if (offsetCurPageView.x < 0 || offsetCurPageView.x > pageView.getPageViewWidth())
                return false;
            WebPDF.Common.preventDefaults(event, true);
            _isMouseDown = true;
            _currentPageView = pageView;
            _beginSelectionPoint = _endSelectionPoint = offsetCurPageView;
            _startwindowpoint.x = event.pageX;
            _startwindowpoint.y = event.pageY;
            _rcDraw = PDFRect(0, 0, 0, 0);
            return true;
        };

        this.onLButtonUp = function (event) {
            if (_isMouseDown) {
                _endwindowpoint.x = event.pageX;
                _endwindowpoint.y = event.pageY;
                _HideBoundBox();
                var pageView = null, offsetCurPageView = new PDF_Point(
						0, 0);
                pageView = WebPDF.TextSelect.CReader_TextSelectTool
						.getPageViewInfoByMouseEvent(_readerApp, event,
								offsetCurPageView, true, false);
                if (pageView == null) {
                    _isMouseDown = false;
                    return false;
                }
                var iPage = -1;
                iPage = pageView.getPageIndex();
                _endSelectionPoint = offsetCurPageView;
                if (iPage != _currentPageView.getPageIndex()) {
                    if (iPage < _currentPageView.getPageIndex()) {
                        _endSelectionPoint.y = 0;
                    }
                    else {
                        _endSelectionPoint.y = _currentPageView.getPageViewHeight();
                    }

                }
                var pdfpoint1 = new PDF_Point(0, 0);
                var pdfpoint2 = new PDF_Point(0, 0);
                pdfpoint1 = _currentPageView.docPtToPDF(_beginSelectionPoint);
                pdfpoint2 = _currentPageView.docPtToPDF(_endSelectionPoint);
                _rcDraw = new PDFRect(pdfpoint1.x, pdfpoint1.y, pdfpoint2.x, pdfpoint2.y);
                WebPDF.RectUtils.normalize(_rcDraw);
                _annotHandleManager.selectAnnots(_currentPageView, _rcDraw);
            }

            _isMouseDown = false;
            WebPDF.RectUtils.empty(_rcDraw);
            _startwindowpoint.x = 0;
            _startwindowpoint.y = 0;
            _endwindowpoint.x = 0;
            _endwindowpoint.y = 0;
            _currentPageView = null;
            return true;
        };

        this.onLButtonDblClk = function (event) {
            return false;
        };

        this.clipSelectRect = function (pageView, rcRect) {
            var rcPage = pageView.getPageViewRect();
            rcRect.left = (rcRect.left > rcPage.left ? rcRect.left : rcPage.left);
            rcRect.top = (rcRect.top > rcPage.top ? rcRect.top : rcPage.top);
            rcRect.right = (rcRect.right > rcPage.right ? rcPage.right : rcRect.right);
            rcRect.bottom = (rcRect.bottom > rcPage.bottom ? rcPage.bottom : rcRect.bottom);
        };


        function _getBoundSelectionRectDiv() {
            if (_boundSelectionRectDiv == null) {
                var docView = _readerApp.getMainView().getDocView();
                var pFirstPageView = docView.getPageView(0);
                var jqPagesContainerDiv = $("#" + docView.getDocViewContainerID());
                var selectionDivID = docView.getDocViewContainerID() + "_annot_bound_select";
                var divHtml = "<div class='fwr-annot_bound_selection fwr-hidden' id='" + selectionDivID + "'></div>";
                if (pFirstPageView != null && pFirstPageView.IsContentCreated()) {
                    $("#" + pFirstPageView.getPageViewContainerID()).parent().parent().append(divHtml);
                }
                else {
                    jqPagesContainerDiv.append(divHtml);
                }
                _boundSelectionRectDiv = $("#" + selectionDivID);
            }
            return _boundSelectionRectDiv;
        };

        function _drawBoundBox() {
            var jqBoundSelection = _getBoundSelectionRectDiv();
            var x = Math.abs(_endwindowpoint.x - _startwindowpoint.x);
            var y = Math.abs(_endwindowpoint.y - _startwindowpoint.y);
            var l = null, t = null;
            // A part
            if (_endwindowpoint.x < _startwindowpoint.x && _endwindowpoint.y < _startwindowpoint.y) {
                l = _endwindowpoint.x;
                t = _endwindowpoint.y;
            }

            // B part
            if (_endwindowpoint.x > _startwindowpoint.x && _endwindowpoint.y < _startwindowpoint.y) {
                l = _startwindowpoint.x;
                t = _endwindowpoint.y;
            }

            // C part
            if (_endwindowpoint.x < _startwindowpoint.x && _endwindowpoint.y > _startwindowpoint.y) {
                l = _endwindowpoint.x;
                t = _startwindowpoint.y;
            }

            // D part
            if (_endwindowpoint.x >= _startwindowpoint.x && _endwindowpoint.y >= _startwindowpoint.y) {
                l = _startwindowpoint.x;
                t = _startwindowpoint.y;
            }

            var docView = _readerApp.getMainView().getDocView();
            var docViewRect = docView.getDocViewClientRect();
            var scrollApi = docView.getScrollApi();
            var scrollPosX = 0, scrollPosY = 0;
            if (ScrollApi) {
                scrollPosX = scrollApi.getContentPositionX();
                scrollPosY = scrollApi.getContentPositionY();
            }

            var rcPage = _currentPageView.GetPageViewRect();

            l = l - docViewRect.left + scrollPosX;
            t = t - docViewRect.bottom + scrollPosY;
            if (l < rcPage.left) {
                x -= rcPage.left - l;
                l = rcPage.left;
            }
            if (t < rcPage.top) {
                y -= rcPage.top - t;
                t = rcPage.top;
            }
            var right = (l + x) > rcPage.right ? rcPage.right : (l + x);
            var bottom = ((t + y) >= rcPage.bottom ? rcPage.bottom - 3 : (t + y));
            x = right - l;
            y = bottom - t;


            jqBoundSelection.css({ left: l, top: t, width: x, height: y });
            jqBoundSelection.removeClass("fwr-hidden");
        };

        function _HideBoundBox() {
            var jqBoundSelection = _getBoundSelectionRectDiv();
            jqBoundSelection.addClass("fwr-hidden");
        };

        this.onMouseMove = function (event) {
            if (_isMouseDown) {
                 WebPDF.Common.PreventDefaults(event, true);
                _endwindowpoint.x = event.pageX;
                _endwindowpoint.y = event.pageY;
                _drawBoundBox();
                //	_rcDraw = new WebPDF.CPDF_Rect(_startwindowpoint.x,_startwindowpoint.y,event.pageX,event.pageY);
                //	_self.ClipSelectRect(_currentPageView,_rcDraw);
                //	_annotHandleManager.ClearDragRects();
                //	_annotHandleManager.AddDragRect(_currentPageView, _rcDraw);
            }
            return true;
        };

        this.onRButtonDown = function (event) {
            return false;
        };

        this.onRButtonUp = function (event) {
            return false;
        };

        this.onRButtonDblClk = function (event) {
            return false;
        };

        this.onMouseWheel = function (event) {
            return false;
        };

        this.onMouseOver = function (event) {
            return false;
        };

        this.onMouseOut = function (event) {
            return false;
        };

        this.onMouseEnter = function (event) {
            return false;
        };

        this.onMouseLeave = function (event) {
            _self.onLButtonUp(event);
            return false;
        };
        this.onKeyDown = function (event) {
            return false;
        };
        this.onKeyUp = function (event) {
            return false;
        };
        this.onChar = function (event) {
            return false;
        }
    };
    return WebPDF.Event.AnnotSelectionToolHandler;
});